package com.bit.stack;

import java.util.Stack;

public class Test2 {
    static class Node{
        public String val;
        public Node next;
        public Node(String val) {
            this.val = val;
            this.next = null;
        }
    }
    //反向打印链表
public static void reversePrint(Node head){
        //递归的写法
//    if (head==null){
//        return;
//    }
//    reversePrint(head.next);
//    System.out.println(head.val+" ");

    //使用栈的写法
    if (head==null){
        return;
    }
    //创建栈，把链表元素都进入栈
    Stack<Node> stack=new  Stack<>();
    for (Node cur=head;cur!=null;cur=cur.next){
        stack.push(cur);
    }
    while (!stack.isEmpty()){
        System.out.println(stack.pop().val);
    }
    }


    public static Node build(){
        Node a=new Node("a");
        Node b=new Node("b");
        Node c=new Node("c");
        Node d=new Node("d");
        a.next=b;
        b.next=c;
        c.next=d;
        return a;
    }
    public static void main(String[] args) {
       Node head=build();
       reversePrint(head);

    }
}
